package org.example.controller;

import org.example.api.CommonResult;
import org.example.model.HairType;
import org.example.service.HairTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/hair")
public class HairTypeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(HairTypeController.class);
    @Autowired
    private HairTypeService hairTypeService;

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult listAll() {
        return CommonResult.success(hairTypeService.listAll());
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody HairType hairType) {
        CommonResult commonResult;
        int count = hairTypeService.create(hairType);
        if (count == 1) {
            commonResult = CommonResult.success(hairType);
        } else {
            commonResult = CommonResult.failed("操作失败");
        }
        return commonResult;
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable("id") Long id, @RequestBody HairType hairType) {
        CommonResult commonResult;
        int count = hairTypeService.update(id, hairType);
        if (count == 1) {
            commonResult = CommonResult.success(hairType);
            LOGGER.debug("updateHairType success:{}", hairType);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateHairType failed:{}", hairType);
        }
        return commonResult;
    }
}
